package com.jie.scanbarcode.demo

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.DialogFragment
import com.jie.scanbarcode.demo.databinding.BarcodeDialogQrCodeResultBinding

/**
 * 显示二维码扫描结果Dialog
 */
typealias ClickListener = () -> Unit

class QRCodeResultDialog : DialogFragment(){

    var againstListener : ClickListener? = null
    var exitListener : ClickListener? = null
    var resultStr : String? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(STYLE_NO_TITLE, android.R.style.Theme_Material_Dialog_Alert)
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
        val viewBinding = BarcodeDialogQrCodeResultBinding.inflate(inflater)
        viewBinding.tvResult.text = resultStr
        viewBinding.btnExit.setOnClickListener {
            dismiss() // 隐藏dialog
            exitListener?.invoke()
        }
        viewBinding.btnAgainst.setOnClickListener{
            dismiss()
            againstListener?.invoke()
        }
        isCancelable = false // 只能点击按钮来取消
        return viewBinding.root
    }

    override fun onDestroyView() {
        super.onDestroyView()
        againstListener = null
        resultStr = null
    }

}